package nl.vu.few.ibivu.biomodels.client.activity;

import nl.vu.few.ibivu.biomodels.client.BioModels;
import nl.vu.few.ibivu.biomodels.client.GatewayServiceAsync;
import nl.vu.few.ibivu.biomodels.client.event.MessageBroadcastType;
import nl.vu.few.ibivu.biomodels.client.ui.view.PreferencesView;
import nl.vu.few.ibivu.biomodels.client.ui.view.PreferencesView.Presenter;
import nl.vu.few.ibivu.biomodels.shared.UserProfile;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.inject.Inject;

public class PreferencesBrowsingActivity extends PresenterActivity implements Presenter {

	PreferencesView view;
	GatewayServiceAsync gs;
	UserProfile userProfile;
	
	@Inject
	public PreferencesBrowsingActivity(EventBus eventBus,
			PlaceController placeController, GatewayServiceAsync gs, PreferencesView view) {
		super(eventBus, placeController);
		this.view = view;
		this.gs = gs;
		this.userProfile = BioModels.getUserInfo().getProfile();
		this.setLoginRequired(true);
	}

	@Override
	public Widget getView() {
		return view.asWidget();
	}

	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view.setPresenter(this);
		panel.setWidget(view);
	}

	@Override
	public String getFirstName() {
		return userProfile.getFirstName();
	}

	@Override
	public String getLastName() {
		return userProfile.getLastName();
	}

	@Override
	public void save() {
		gs.saveUserProfile(userProfile, new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				broadcastMessage(MessageBroadcastType.ERROR, "You must log in!");
			}

			@Override
			public void onSuccess(Void result) {
				broadcastMessage(MessageBroadcastType.INFO, "Preferences saved!");
			}});
	}

	@Override
	public void setFirstName(String firstName) {
		userProfile.setFirstName(firstName);
		
	}

	@Override
	public void setLastName(String lastName) {
		userProfile.setLastName(lastName);
	}

}
